function itService (){
	this.location = "http://nalijepi.me/BI/";
	
}

itService.prototype.GetData = function(jsonVar, f2, textOutput){
	$.post(this.location + "itSmallBI/index.php/getdata", 
	JSON.stringify(jsonVar),
			function(data){
                console.log(data);
				data  = JSON.parse(data);
                
				keys = data.keys;
				
				results = data.results;
				data = data.results;
				
				//console.log(results);
				
				keyValues = new Array();
				
				valueKey = "";
				
				for(var key in keys)
				{
					if (key == keys.length-1)
					{
						valueKey = key;
						continue;
					}
					keyValues.push(new Array());
					keyValues[key].keyName = keys[key];
					
					keyValues[key].values = new Array();
					
					//Dodavanje funkcije contains array objektu values
					keyValues[key].values.contains = function(obj) {
						var i = this.length;
						while (i--) {
							if (this[i] == obj) {
								return true;
							}
						}
						return false;
					}
					
					
					for (var result in results)
					{
						//result -> indeks u polju results
						
						//keys[key] -> property (dimenzija, mjera)
						//data -> polje zapisa
						//data[result] -> zapis na adresi result
						//data[result][keys[key]] -> zapis na adresi result sa propertyem keys[key]
						
						if (! keyValues[key].values.contains(data[result][keys[key]]) )
						{
							keyValues[key].values.push(data[result][keys[key]]);
						}
					}
					//keyValues.push(data[key]);
				}
				
				//console.log("Key Values:");
				//console.log(keyValues);
				
				//console.log(data);
				
				counter = new Array();
				repeats = 1;
				
				// inicijalizacija counter-a [0 , 0, 0, , ..0] ovisni koliko ima keyValue-a
				for(a in keyValues)
				{
					counter[a] = 0;
					//console.log(keyValues[a]);
					//console.log(keyValues[a]['values']['length']);
					repeats = repeats * keyValues[a]['values']['length'];
				}
				
				function checkCounter(counter, position){
					if (counter[position] == keyValues[position].values.length)
					{
						counter[position] = 0;
						if (position != 0)
						{
							counter[position-1]++;
							
							counter = checkCounter(counter, position -1);
						}
					}
					return counter;
				}
				//console.log(repeats);
				var cnt = new Array();
				
				for(i=0; i<repeats; i++)
				{
				cnt.push(jQuery.extend({}, counter));
					counter[counter.length -1]++;
					
					//console.log(counter);
					counter = checkCounter(counter, counter.length-1);
					
				}
				
				//console.log(cnt);
				
				dataItems = new Array();
				
				for(key in cnt)
				{
					obj = {};
					for(key2 in cnt[key])
					{
						obj[keyValues[key2]['keyName']] = keyValues[key2]['values'][cnt[key][key2]];
					}
					//console.log(obj);
					
					for(k in results)
					{
						//console.log(results[k]);
						same = true;
						for (l in obj)
						{
							if(results[k][l] != obj[l])
							{
								//console.log("Key: " + l + " obj[key] :" + obj[l] + " results[k][l] :" + results[k][l]);
								//console.log(l);
								//console.log(results[k][l]);
								same = false;
							}
						}
						Object.keys = Object.keys || function(o) {  
							var result = [];  
							for(var name in o) {  
								if (o.hasOwnProperty(name))  
								  result.push(name);  
							}  
							return result;  
						};

						if (same)
						{
							//console.log("nes radi");
							//console.log(results[k][Object.keys(results[k])[Object.keys(results[k]).length-1]]);
							obj.value = parseFloat(results[k][Object.keys(results[k])[Object.keys(results[k]).length-1]]);
							break;
						
						}
						/*
						for(l in results[k])
						{
							console.log(l);
							console.log(results[k][l]);
						}*/
					}
					if (!obj.hasOwnProperty("value"))
					{
						obj.value = 0;
					}
					
					dataItems.push(obj);
					//console.log(obj);
					
					
				}
                //console.log(dataItems);
                textOutput(dataItems);
				//console.log(keyValues[0].values.length);
				
				same = 1;
				
				Object.keys = Object.keys || function(o) {  
					var result = [];  
					for(var name in o) {  
						if (o.hasOwnProperty(name))  
						  result.push(name);  
					}  
					return result;  
				};
				
				
				key2 = Object.keys(dataItems[0])[0];
				
				for(key in dataItems)
				{
					//console.log(dataItems[key][key2] + dataItems[key-1][key2]);
					if (key != 0)
					{
						if (dataItems[key][key2] == dataItems[key-1][key2])
						{
							same++;
						}
						else
						{
							break;
						}
					}
				}
				
				outData = new Array();
				//console.log(typeof(outData));
				for ( key in dataItems)
				{
				//console.log(dataItems[key]['value'] + ' ' + key%same);
					if (typeof(outData[key % same]) != "object")
					{
						outData[key % same] = new Array();
					}
					outData[key % same].push(dataItems[key]['value']);
				}
				//console.log(same);
				//console.log(outData);
				f2(outData);
				returnData = outData;
			}
		);
}
